//
//  CellTwo.m
//  project-A (pianke)
//
//  Created by dllo on 16/1/25.
//  Copyright © 2016年 scott. All rights reserved.
//

#import "CellTwo.h"
#import "CellTwoTable.h"
#import "ModelOfTravl.h"
#import "NetworkingHandle.h"
#import <UIImageView+WebCache.h>
#import <MJRefresh.h>
#import "specialImage.h"
#import "InfoViewController.h"

#import <AFNetworking.h>
#define WIDTH self.frame.size.width

#define HEIGHT self.frame.size.height

@interface CellTwo ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,retain)NSMutableArray *arr;

@property (nonatomic,assign)NSInteger number;

@end

@implementation CellTwo


-(void)dealloc{
    [_tabview release];
    [_arr release];
    [super dealloc];
}

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.number = 2;
        
        self.tabview.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor blackColor]);
        
      
        
        self.tabview = [[UITableView alloc]init];
        
        [self.contentView addSubview:self.tabview];
        
        
        self.tabview.backgroundColor = [UIColor lightGrayColor];
        
        self.tabview.delegate = self;
        self.tabview.dataSource = self;
        
        self.tabview.contentInset = UIEdgeInsetsMake(0, 0, 64, 0);
        
        
        self.tabview.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.tabview registerClass:[CellTwoTable class] forCellReuseIdentifier:@"pool1"];
        
               
        [self.tabview release];
    }
    
    return self;
}

-(void)geturlasd{
    
    NSString *str = @"http://chanyouji.com/api/trips/featured.json?page=1";

    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    

    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
    
    NSURL *URL = [NSURL URLWithString:str];

    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        if (error) {
            NSLog(@"阿萨大大的");
        } else {
            self.arr = [NSMutableArray array];
            for (NSDictionary *dic in responseObject) {
//                NSLog(@"%@",responseObject);
                
                ModelOfTravl *model = [[ModelOfTravl alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                
                model.idd = [NSString stringWithFormat:@"%@",[dic objectForKey:@"id"]];
                
                [self.arr addObject:model];
                [model release];
            }
            [self.tabview reloadData];
        }
    }];
    [dataTask resume];

   
   }


    


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.arr.count;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return WIDTH * 0.55 + 8;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ModelOfTravl *model = [self.arr objectAtIndex:indexPath.row];
    CellTwoTable *cell = [[CellTwoTable alloc]init];
    cell.name.text = model.name;
   
    NSString *str = [NSString stringWithFormat:@"%ld",model.days];
    NSString *str2 = [NSString stringWithFormat:@"%ld",model.photos_count];
    cell.date.text = [NSString stringWithFormat:@"%@ / %@天,%@图",model.start_date,str,str2];
    
    [cell.image sd_setImageWithURL:[NSURL URLWithString:model.front_cover_photo_url] placeholderImage:nil options:SDWebImageAvoidAutoSetImage];
    
    
    [cell.specialview.special sd_setImageWithURL:[NSURL URLWithString:[model.user objectForKey:@"image"]] placeholderImage:nil options:SDWebImageAvoidAutoSetImage];
    
    
    cell.specialview.block = ^(){
        
        [self.delegate didSeletedWith:[self.arr objectAtIndex:indexPath.row]];
            };

    
    cell.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor blackColor]);
    
    return cell;
    
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSLog(@"%@",indexPath);
    [self.delegate didSeletedTripWith:[self.arr objectAtIndex:indexPath.row]];
   
}

-(void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes{
    self.tabview.frame = self.contentView.frame;
    
}
-(UICollectionViewLayoutAttributes *)preferredLayoutAttributesFittingAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes{
    
    
    
  
    
    return  layoutAttributes;
    
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
   }

@end
